package com.nobody.facepk.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nobody.facepk.model.Match;
import com.nobody.facepk.model.MatchData;
import com.nobody.facepk.model.MatchDataType;
import com.nobody.facepk.model.Picture;
import com.nobody.facepk.model.PictureData;
import com.nobody.facepk.model.PictureDataType;
import com.nobody.facepk.model.PictureState;
import com.trg.dao.hibernate.GeneralDAO;
import com.trg.search.Search;

@Component
public class PictureDao {
	private GeneralDAO generalDao;
	
	public List<Picture> getPictures(Long userId){
		Search search = new Search(Picture.class);
		search.addFilterEqual("userId", userId);
		return this.generalDao.search(search);
	}
	
	public List<Picture> getScoredPictures(Long userId){
		Search search = new Search(Picture.class);
		search.addFilterEqual("userId", userId);
		search.setMaxResults(10);
		return this.generalDao.search(search);
	}
	
	public List<Picture> getScoringPictures(Long userId){
		Search search = new Search(Picture.class);
		search.addFilterEqual("userId", userId);
		search.addFilterEqual("state", PictureState.SCORING);
		search.setMaxResults(10);
		return this.generalDao.search(search);
	}
	public List<Picture> getMyInitPictures(Long userId){
		Search search = new Search(Picture.class);
		search.addFilterEqual("userId", userId);
		search.addFilterEqual("state", PictureState.INIT);
		search.setMaxResults(10);
		return this.generalDao.search(search);
	}
	
	@Transactional(readOnly=false)
	public List<Picture> getFreePictures(Long userId){
		Search search = new Search(Picture.class);
		search.addFilterEqual("userId", userId);
		search.addFilterEqual("state", PictureState.FREE);
		return this.generalDao.search(search);
	}
	public List<Picture> getNearlyMatchPic(Long userId,Picture pic,int maxResult){
		//我目前照片的得分
		int score = pic.getScore();
		//取到比我个分数高的一条数据
		Search search = new Search(Picture.class);
		search.addFilterNotEqual("userId", userId);
		search.addFilterEqual("sex", pic.getSex());
		search.addFilterEqual("state", PictureState.FREE);
		search.addFilterGreaterThan("score", score);
		search.addSort("score", false);
		search.setMaxResults(maxResult);
		return this.generalDao.search(search);
	}
	
//	public Set<Long> getRelatedScoringPictureIds(Long userId){
//		User user = this.generalDao.find(User.class, userId);
//		Friends friends = this.generalDao.find(Friends.class,user.getFriendId());
//		Set<Long> friendIds = friends.getFriendIds();
//		
//		Set<Long> pictures = new HashSet<Long>();
//		for(Long frienId : friendIds) {
//			User friend = this.generalDao.find(User.class, frienId);
//			List<Long> pics = friend.getScoringPictureIds();
//			pictures.addAll(pics);
//		}
//		return pictures;
//	}
	
	public MatchData getMatchData(Long matchId, MatchDataType type) {
		Search search = new Search(MatchData.class);
		search.addFilterEqual("matchId", matchId);
		search.addFilterEqual("type", type);
		return (MatchData)this.generalDao.searchUnique(search);
	}
	
	public PictureData getPictureData(Long pictureId, PictureDataType type) {
		Search search = new Search(PictureData.class);
		search.addFilterEqual("pictureId", pictureId);
		search.addFilterEqual("type", type);
		return (PictureData)this.generalDao.searchUnique(search);
	}
	
	@Autowired
	public void setGeneralDao(GeneralDAO generalDao) {
		this.generalDao = generalDao;
	}
}
